package com.infore.airsupervise.biz.impl;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.PointQueryE;
import com.infore.airsupervise.bean.PolluterE;
import com.infore.airsupervise.biz.PolluterBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * 污染源业务接口实现
 * <p>
 * 作者: dingxinlin
 * 创建日期: 2017/4/12 15:41
 */

public class PolluterBizImpl implements PolluterBiz {

    /**
     * 搜索污染源
     * @param type
     * @param keyword
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> searchPolluter(
            int type, String keyword, final OnServerResponseListener<List<PolluterE>> listener) {

        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        // 参数
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("state", type);
        parameters.put("keyword", keyword);

        // 请求
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.SEARCH_POLLUTER, parameters);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<PolluterE> beanList = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, PolluterE.class);
                listener.onSucceed(beanList, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    /**
     * 获取污染源详情
     * @param mnCode
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> getPolluterDetail(String mnCode, final OnServerResponseListener<PolluterE> listener) {
        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        // 参数
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("mn", mnCode);

        // 请求
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_POLLUTER_DETAIL, parameters);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                PolluterE bean = JSONUtil.parseBean(
                        response.getData(), Constants.RESPONSE_BEAN_KEY, PolluterE.class);
                listener.onSucceed(bean, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    @Override
    public Call<BaseResponse> getPolluterList(final OnServerResponseListener<List<PolluterE>> listener) {

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_POLLUTER_LIST, Collections.EMPTY_MAP);
        call.enqueue( new HttpCallBack() {
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<PolluterE> beanList = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, PolluterE.class);
                listener.onSucceed(beanList, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }
}
